import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import '../../resources/kq_theme_colors.dart';
import 'package:kq_flutter_core_widget/utils/text_filed_utils.dart';

import '../../theme/kq_theme_manager.dart';
import '../button/kq_swith_button.dart';
import '../divider/kq_divider.dart';
import 'entity/kq_form_entity.dart';

/// 通用表单组件 - 开关组件
///
/// [ItemType.switcher]
///
/// @author 周卓
///
/// 结合 KqFormEntity 使用
/// 左边必填 + 标题 + 右侧开关
class KqSwitcherItemView extends StatefulWidget {
  /// 通用属性
  final KqFormEntity entity;

  /// 变化监听
  final ValueChanged<bool>? onCheckedChanged;

  /// 自定义控件的底部控件
  final Widget Function(KqFormEntity entity)? customBottomView;

  const KqSwitcherItemView(
      {Key? key,
      required this.entity,
      this.onCheckedChanged,
      this.customBottomView})
      : super(key: key);

  @override
  KqSwitcherItemViewState createState() => KqSwitcherItemViewState();
}

class KqSwitcherItemViewState extends State<KqSwitcherItemView> {
  late TextEditingController _controller;

  @override
  void initState() {
    widget.entity.widgetState = this;

    _controller = widget.entity.controller ?? TextEditingController();
    _controller.text = widget.entity.valueForShow ?? '';

    super.initState();
  }

  /// 主动刷新方法
  update() {
    if (mounted) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    // 如果外层实体是反复重新创建的，则需要在此重新赋值
    widget.entity.widgetState = this;
    // 背景颜色
    var bgColor = widget.entity.backgroundColor ??
        KqThemeManager.getFormConfig().backgroundColor ??
        KqThemeColors.bgWhite;
    return Visibility(
      visible: widget.entity.visible,
      child: Column(
        children: [
          InkWell(
            onTap: widget.entity.isEditable()
                ? () {
                    TextFiledUtil.clearFocus(context);
                    setState(() {
                      widget.entity.checked = !widget.entity.checked;
                      widget.onCheckedChanged?.call(widget.entity.checked);
                      widget.entity.onCheckedChanged
                          ?.call(widget.entity.checked);
                    });
                  }
                : null,
            child: Container(
              color: bgColor,
              padding: EdgeInsets.only(
                  right: widget.entity.paddingHorizontal ??
                      KqThemeManager.instance
                          .getConfig()
                          .formConfig
                          .paddingHorizontal ??
                      16.r),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      Container(
                        margin: EdgeInsets.only(
                            top: widget.entity.paddingVertical ??
                                KqThemeManager.instance
                                    .getConfig()
                                    .formConfig
                                    .paddingVertical ??
                                15.r,
                            bottom: widget.entity.paddingVertical ??
                                KqThemeManager.instance
                                    .getConfig()
                                    .formConfig
                                    .paddingVertical ??
                                15.r),
                        padding: EdgeInsets.only(
                            right: widget.entity.titleRightMargin ??
                                (widget.entity.title.isNotEmpty ? 10.r : 0)),
                        child: Row(
                          children: <Widget>[
                            ///是否必填
                            Container(
                              alignment: Alignment.centerRight,
                              width: widget.entity.paddingHorizontal ??
                                  KqThemeManager.instance
                                      .getConfig()
                                      .formConfig
                                      .paddingHorizontal ??
                                  16.r,
                              padding: EdgeInsets.only(right: 2.r),
                              child: Text(
                                "∗",
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                    fontSize: KqThemeManager.instance
                                            .getConfig()
                                            .formConfig
                                            .mustInputFontSize ??
                                        14.sp,
                                    color: (widget.entity.mustInput &&
                                                widget.entity.isEditable()) ||
                                            (!widget.entity.isEditable() &&
                                                widget
                                                    .entity.forceShowMushInput)
                                        ? KqThemeColors.textRed
                                        : KqThemeColors.bgTransparent),
                              ),
                            ),

                            ///标题
                            LimitedBox(
                              maxWidth: KqThemeManager.instance
                                      .getConfig()
                                      .formConfig
                                      .titleMaxWidth ??
                                  200.r,
                              child: Text(
                                widget.entity.title,
                                maxLines: widget.entity.multiline ? null : 1,
                                overflow: widget.entity.multiline
                                    ? null
                                    : TextOverflow.ellipsis,
                                style: widget.entity.titleStyle ??
                                    TextStyle(
                                        fontSize: KqThemeManager.instance
                                                .getConfig()
                                                .formConfig
                                                .titleFontSize ??
                                            16.sp,
                                        color: widget.entity.titleColor ??
                                            KqThemeManager.getFormConfig()
                                                .titleTextColor ??
                                            KqThemeColors.text26),
                              ),
                            )
                          ],
                        ),
                      ),
                      Expanded(
                        /// 开关
                        child: DefaultTextStyle(
                          style: TextStyle(
                              fontSize: KqThemeManager.instance
                                      .getConfig()
                                      .formConfig
                                      .titleFontSize ??
                                  14.sp,
                              color: widget.entity.contentColor ??
                                  KqThemeManager.getFormConfig()
                                      .contentTextColor ??
                                  KqThemeColors.text59),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              widget.entity.switcherBuilder
                                      ?.call(widget.entity) ??
                                  KqSwitchButton(
                                      size: widget.entity.switcherBtnSize,
                                      value: widget.entity.checked,
                                      disabled: true)
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),

          /// 自定义底部控件，来自实体类
          widget.entity.customBottomView != null
              ? widget.entity.customBottomView!(widget.entity)
              : Container(),

          /// 自定义控件，来自控件
          widget.customBottomView != null
              ? widget.customBottomView!(widget.entity)
              : Container(),

          /// 分割线
          Visibility(
              visible: !widget.entity.hideDivider, child: KqDivider(
            leftMargin: !widget.entity.dividerHorMarginEnable ? 0 : widget.entity.paddingHorizontal ??
                KqThemeManager.instance
                    .getConfig()
                    .formConfig
                    .paddingHorizontal ??
                16.r,
            rightMargin: !widget.entity.dividerHorMarginEnable ? 0 : widget.entity.paddingHorizontal ??
                KqThemeManager.instance
                    .getConfig()
                    .formConfig
                    .paddingHorizontal ??
                16.r,
          ))
        ],
      ),
    );
  }
}
